﻿using UnityEngine;
using System.Collections;
using LitJson;

public class SubmitChangeButton : MonoBehaviour {

	private bool m_submit = false;

	void OnClick()
	{
		if (!m_submit)
		{
			m_submit = true;
			
			Debug.LogError(MyInfo.Instance.LastSelectedUnitDBID);
			
			if( MyInfo.Instance.LastSelectedUnitDBID != 0 )
			{
	            UnitInfo unitInfo = MyInfo.Instance.GetUnitInfoById(MyInfo.Instance.LastSelectedUnitDBID);
	
	            MyInfo.Instance.MyDeckMap[MyInfo.Instance.MyCurrentDeck].SlotMap[MyInfo.Instance.LastSeletedUnitSlot] = unitInfo;
			}

            LoadingWindow.window.ShowShortLoading();
			RestAPI.singleton.SendMyDeckChangeInfo(ReturnCallback, MyInfo.Instance.MyDeckMap[MyInfo.Instance.MyCurrentDeck].deckId);
		}
	}

    void ReturnCallback(Hashtable returnData)
    {
        LoadingWindow.window.HideLoading();
		if( MyInfo.Instance.LastSelectedUnitDBID == 0 )
		{
			UnitInfo info = new UnitInfo(0,0,0,0,0);
			MyInfo.Instance.MyDeckMap[MyInfo.Instance.MyCurrentDeck].SlotMap[MyInfo.Instance.LastSeletedUnitSlot] = info;
		}
        CSceneManager.manager.GoToDeckEditScene();
    }
}
